package cn.cnf.controller;

import cn.cnf.common.Result;
import cn.cnf.common.enums.StatusCode;
import cn.cnf.common.exception.CustomException;
import cn.cnf.common.utils.UserRequestUtil;
import cn.cnf.entity.Subscribe;
import cn.cnf.service.SubscribeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 服务控制器
 *
 * @author ZhuMeiYi
 * @description
 * @since 2020-12-25 12:19:41
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/subscribe")
public class SubscribeController {
    @Autowired
    private SubscribeService subscribeService;

    /**
     * @return
     */
    @GetMapping("/confirm")
    public Result confirm(Subscribe subscribe) {
        try {
            return Result.success(subscribeService.confirmSubscribe(subscribe));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 取消订阅
     *
     * @param subscribe
     * @return
     */
    @GetMapping("/cancel")
    public Result cancel(Subscribe subscribe) {
        try {
            return Result.success(subscribeService.cancelSubscribe(subscribe));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    @GetMapping("/allComic")
    public Result getAllComic() {
        try {
            return Result.success(subscribeService.getAllComic());
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }
    @GetMapping("/allNovel")
    public Result getAllNovel() {
        try {
            return Result.success(subscribeService.getAllNovel());
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 是否订阅了某作品
     *
     * @param subscribeId
     * @return
     */
    @GetMapping("/ifSubscribe")
    public Result ifSubscribe(@RequestParam("subscribeId") String subscribeId, @RequestParam("chapterId") String chapterId) {
        try {
            return Result.success(subscribeService.ifSubscribe(UserRequestUtil.getCurrentUserName(), subscribeId, chapterId));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }
}